package com.web.biz.grgn.service;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.exception.SwordBaseCheckedException;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;
import com.web.common.dataDictionary.DE.DE_C_BRAND_OPPTIME;
import com.web.common.dataDictionary.DE.DE_C_BRAND_XTBPATH;
import com.web.common.dataDictionary.DE.DE_SHFJ;
import com.web.common.dataDictionary.DE.DE_SHSJ;
import com.web.common.dataDictionary.DE.DE_SHZT;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.dataDictionary.PO.TabUserPO;
import com.web.common.utils.CacheUtil;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.FileUploadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class GrxxShService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(GrxxShService.class);

	public GrxxShService() throws Exception {
		super();
	}


	/**
	 * 初始化个人信息审核页面
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "GrxxShService_initGrxxSh")
	public IResData initGrxxSh(IReqData req) throws Exception {
		
		log.debug("开始进入 GrxxShService_initGrxxSh 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		IResData grshxxres = getGrshxx(dao);
		
		res.joinRes(grshxxres);
		res.addPage("/pages/biz/grxxsh/grxxshForm.jsp");
		return res;

	}

	/**
	 * 用户提交审核信息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "GrxxShService_submit")
	public IResData submit(IReqData req) throws Exception {

		log.debug("开始进入 GrxxShService_submit 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		IResData res = new SwordRes();
		String flag="false";
		try {
			//凭证附件
			BaseDomainCtrl domainCtrl = BaseDomainCtrl.class.newInstance();
			List<SwordFileItem> pzfjlist = domainCtrl.getFormUploadList2(req, "form_grxxsh", "pzfj");
			String pzfj = FileUploadUtiles.uploadFileAndGetFileXML(pzfjlist,FileUploadUtiles.DOMAIN_SHOPXXSH);
			//shoplogo附件
			List<SwordFileItem> tbfjlist = domainCtrl.getFormUploadList2(req, "form_grxxsh", "tbfj");
			String tbfj = FileUploadUtiles.uploadFileAndGetFileXML(tbfjlist,FileUploadUtiles.DOMAIN_SHOPLOGO);
			//更新个人信息
			TabUserPO po = (TabUserPO) req.getForm("form_grxxsh",TabUserPO.class);
			po.setShfj(new DE_SHFJ(pzfj));
			po.setShzt(new DE_SHZT("dsh"));
			po.setShsj(new DE_SHSJ(DateFormatUtil.getNowDate()));
			int i = dao.updatePO(po);
			//更新shop信息
			TCategoryShopPO shoppo = (TCategoryShopPO) req.getForm("form_grxxsh", TCategoryShopPO.class);
			shoppo.setCBrandXtbpath(new DE_C_BRAND_XTBPATH(tbfj));
			shoppo.setCBrandOpptime(new DE_C_BRAND_OPPTIME(DateFormatUtil.getNowDate()));
			int j = dao.updatePO(shoppo);
			if (i > 0 && j>0) {
				dao.commitAll();
				flag="true";
			}
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
			flag="false";
		}
		
		res.addAttr("flag",flag);
		return res;
	}

	/**
	 * 抽取的公共方法查询用户的审核信息
	 * @param dao
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private IResData getGrshxx(IPersistenceService dao) throws SwordBaseCheckedException {
		IResData res = new SwordRes();
		String userName = LoginInfoUtil.getLoginUserName();
		SQLParam param = new SQLParam();
		param.addParam(userName);
		Map<String, Object> usermap = dao.queryMapByKey("LoginRegisterService_queryByYhm", param);
		Map<String, Object> shopmap = dao.queryMapByKey("grxx_queryShopxxByYhm", param);
		String shzt = StringUtil.parse2String(usermap.get("shzt"));
		
		/*//凭证附件
		String fj = StringUtil.parse2String(usermap.get("shfj"));
		List<Map> fjlist = FileDownloadUtiles.getfileDownloadMap(fj);
		if(StringUtils.isNotEmpty(fj) && CollectionUtil.isNotBlankList(fjlist)){
			res.addListWithDataName("pzfj", FileDownloadUtiles.getfileDownloadMap(fj));
		}
		//shoplogo附件
		String tbfj = StringUtil.parse2String(shopmap.get("cBrandXtbpath"));
		List<Map> fjlist2 = FileDownloadUtiles.getfileDownloadMap(fj);
		if(StringUtils.isNotEmpty(tbfj) && CollectionUtil.isNotBlankList(fjlist2)){
			res.addListWithDataName("tbfj", FileDownloadUtiles.getfileDownloadMap(tbfj));
		}*/
		shopmap.putAll(usermap);
		//List categoryList = CacheUtil.getPullData("DM_YW_CATEGORY");
		List list = dao.queryMapListByKey("queryTcategory", null);
		res.addListWithDataName("categoryTitle", list);
		res.addForm("form_grxxsh", shopmap);
		res.addJSTL("shzt", shzt);
		res.addJSTL("vipsyts", StringUtil.parse2String(usermap.get("vipsyts")));
		return res;
	}
}